$(function(){
	var reqDepartment = -1;
    var reqSkills = new Array;
    $("#memberlist .member-select").hide();
    $("#memberlist .member-checkbox:checked").each(function(){
        $(this).parent().find('.member-select').show();
    });
    $("#memberlist .member-checkbox").change(function(){
        if($(this).is(":checked")){
            $(this).parent().find('.member-select').show();
        }else{
            $(this).parent().find('.member-select').hide();
        }
		setSameSize();
    });
    $(".department").change(function(){
        if($(this).is(":checked")){
            reqDepartment = $(this).val();            
        }
		$("#skill_filter input").val([]);
		$("#skill_filter input").change();
		applyUserlistFilters();
    });
	
    $(".skill").change(function(){
        if($(this).is(":checked")){
            reqSkills.push($(this).val());            
        }else{
            reqSkills.splice(reqSkills.indexOf($(this).val()),1);
        }
		applyUserlistFilters();
    });
	
	applyUserlistFilters();
	setSameSize();
	
	
    function setSameSize(){
		$("#projectDetails, #projectMembers").height('auto');
		var height = Math.max($("#projectDetails").height(), $("#projectMembers").height());
		$("#projectDetails, #projectMembers").height(height);
	}
  
   function applyUserlistFilters(){
	   //zobrazit všechny lidi, skrýt všechny skilly
	   $("#memberlist .member-item").show();
	   $("#skill_filter .skill_item").hide();
	   $("#skill_filter").hide();
	   if(reqDepartment != -1){
		   //zobrazit všechny skilly
		   $("#skill_filter .skill_item").show();
		   $("#skill_filter").show();
		   setSameSize();
		   //skrýt všude vše mimo oddělení
		   $("#memberlist input").each(function(){
               if(usersInDepartment.hasOwnProperty(reqDepartment)){
				   if(usersInDepartment[reqDepartment].indexOf($(this).val()) == -1){
					   $(this).parent('.member-item').hide();
				   }
			   }
           });
		   $("#skill_filter input").each(function(){
               if(skillsInDepartment.hasOwnProperty(reqDepartment)){
				   if(skillsInDepartment[reqDepartment].indexOf($(this).val()) == -1){
					   $(this).parent().hide();
				   }
			   }
           });
		   if(reqSkills.length > 0){
			   $("#memberlist input:visible").each(function(){
				   var validMember = true;
				   for( i in reqSkills){
					   if(usersWithSkill.hasOwnProperty(reqSkills[i])){
						   if(usersWithSkill[reqSkills[i]].indexOf($(this).val()) == -1){
							   validMember = false;
							   break;
						   }
					   }
				   }
				   if(validMember == false){
					   $(this).parent('.member-item').hide();
				   }
			   });
		   }
	   }
	}
   
});

	


$(document).ready(function () {
    $.datepicker.regional['cs'] = {
        closeText: 'Zavřít',
        prevText: '&#x3c;Dříve',
        nextText: 'Později&#x3e;',
        currentText: 'Nyní',
        monthNames: ['leden', 'únor', 'březen', 'duben', 'květen', 'červen', 'červenec', 'srpen',
            'září', 'říjen', 'listopad', 'prosinec'],
        monthNamesShort: ['led', 'úno', 'bře', 'dub', 'kvě', 'čer', 'čvc', 'srp', 'zář', 'říj', 'lis', 'pro'],
        dayNames: ['neděle', 'pondělí', 'úterý', 'středa', 'čtvrtek', 'pátek', 'sobota'],
        dayNamesShort: ['ne', 'po', 'út', 'st', 'čt', 'pá', 'so'],
        dayNamesMin: ['ne', 'po', 'út', 'st', 'čt', 'pá', 'so'],
        weekHeader: 'Týd',
        dateFormat: 'dd. mm. yy',
        firstDay: 1,
        isRTL: false,
        showMonthAfterYear: false,
        yearSuffix: ''
    };
    
    $.datepicker.setDefaults($.datepicker.regional['cs']);
    
    $("input.date").each(function () { // input[type=date] does not work in IE
        var el = $(this);
        var value = el.val();
        var date = (value ? $.datepicker.parseDate($.datepicker.W3C, value) : null);

        var minDate = el.attr("min") || null;
        if (minDate) minDate = $.datepicker.parseDate($.datepicker.W3C, minDate);
        var maxDate = el.attr("max") || null;
        if (maxDate) maxDate = $.datepicker.parseDate($.datepicker.W3C, maxDate);

        // input.attr("type", "text") throws exception
        if (el.attr("type") == "date") {
            var tmp = $("<input/>");
            $.each("class,disabled,id,maxlength,name,readonly,required,size,style,tabindex,title,value".split(","), function(i, attr)  {
                tmp.attr(attr, el.attr(attr));
            });
            el.replaceWith(tmp);
            el = tmp;
        }
        el.datepicker({
            minDate: minDate,
            maxDate: maxDate
        });
        el.val($.datepicker.formatDate(el.datepicker("option", "dateFormat"), date));
    });
});

